package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyContentService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyContentService contentService;
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyDetailQueryObject qo, Model model){
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("strategys",strategyService.selectAll());
        return "/strategyDetail/list";
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(StrategyDetail strategyDetail, MultipartFile file){
        if (file != null&& file.getSize()>0) {
            String coverUrl = UploadUtil.upload(file, UploadUtil.PATH+"/upload");
            strategyDetail.setCoverUrl(coverUrl);
        }
        strategyDetailService.saveOrUpdate(strategyDetail);
        return new JSONResult();
    }

    @GetMapping
    @ResponseBody
    public StrategyContent getContentByDetailId(Long id){
        return contentService.selectByDetailId(id);
    }
}
